package com.woniu.controller;

import com.woniu.common.CommonResult;
import com.woniu.entity.Areainfo;
import com.woniu.entity.PersonUser;
import com.woniu.entity.PersonUserAddress;
import com.woniu.service.AreainfoService;
import com.woniu.service.PersonUserAddressService;
import com.woniu.service.PersonUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.standard.expression.Each;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户地址表(PersonUserAddress)表控制层
 *
 * @author makejava
 * @since 2020-03-26 18:56:24
 */
@Controller
@RequestMapping("/personUserAddress")
public class PersonUserAddressController {
    /**
     * 服务对象
     */
    @Resource
    private PersonUserAddressService personUserAddressService;

    @Resource
    PersonUserService personUserService;

    @Resource
    AreainfoService areainfoService;

    /**
     * 功能描述: 查询当前用户的收货地址
     * @Param: []
     * @Return: com.woniu.common.CommonResult
     * @Author: yanghan
     * @Date: 2020/3/26 19:31
     */
    @GetMapping("/findAddressByUserId")
    public @ResponseBody CommonResult findAddressByUserId(){
        //获取当前登录的用户的用户名
//        String name = cookieUtil.getName();
        String name = "admin";
        //查询用户Id
        PersonUser personUser = personUserService.findIdByName(name);
        Integer userId = personUser.getId();
        //根据用户Id查询详细地址
        List<PersonUserAddress> address = personUserAddressService.findAllAddressByUserId(userId);
        //根据用户Id查询用户信息
        PersonUser user = personUserService.queryById(userId);
        Map<Object, Object> map = new HashMap<>();
        map.put("user",user);
        map.put("address",address);
        return CommonResult.success(map);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public PersonUserAddress selectOne(Integer id) {
        return this.personUserAddressService.queryById(id);
    }

}